#!/usr/bin/python
# -*- coding: utf-8 -*-

from web.route import Router
from utils.common import import_cls
from web.response import Response
from web.request import Request

def application(environ, start_response):  

    router = Router()  

    @router('')  
    def index(req, resp):  
        cls = import_cls('wsgi.index.Index')
        return cls().handle(req, resp)

    
    @router('/query')  
    def query(req, resp):  
        cls = import_cls('wsgi.query.Query')
        return cls().handle(req, resp)

    @router('/query/handle')  
    def query_handle(req, resp):  
        cls = import_cls('wsgi.query.QueryHandler')
        return cls().handle(req, resp)

    @router('/stats')  
    def stats(req, resp):  
        cls = import_cls('wsgi.stats.Stats')
        return cls().handle(req, resp)

    @router('/stats/handle')  
    def stats_handle(req, resp):  
        cls = import_cls('wsgi.stats.StatsHandler')
        return cls().handle(req, resp)

    @router('/login')  
    def login(req, resp):  
        cls = import_cls('wsgi.login.Login')
        return cls().handle(req, resp)

    @router('/login/handle')  
    def login_handle(req, resp):  
        cls = import_cls('wsgi.login.LoginHandler')
        return cls().handle(req, resp)

    @router('/logout')  
    def logout_handle(req, resp):  
        cls = import_cls('wsgi.logout.LogoutHandler')
        return cls().handle(req, resp)

    @router('/error')  
    def error(req, resp):  
        cls = import_cls('wsgi.error.Error')
        return cls().handle(req, resp)

    @router('/notfound')  
    def error(req, resp):  
        cls = import_cls('wsgi.error.NotFound')
        return cls().handle(req, resp)

    ################################
    ###根据路径路由到相应程序处理###
    ###############################
    req = Request(environ)
    resp = Response(start_response)
    return router.route(req, resp)   


        


